package GameEngine;


import Main.*;

public class AreaController
{
	/**
	 * Returns whether the given string can be numeric or not
	 */
	public boolean isNumeric(String str)
	{
		try{
			Integer.parseInt(str);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	/**
	 * Checks the intersection between the boat and other objects.
	 * Execute the neccesary methods if the objects and the boat are intersect.
	 */
	public void checkIntersection()
	{
		int ulx = (int) (Main.mygame.getBoat().getLocation().getX()-100)/50;
		int uly = (int) (Main.mygame.getBoat().getLocation().getY())/44;
		
		int densitiesR = (Main.mygame.getBoat().getLocation().getX()%50)*2;
		
		if (ulx==10)
			ulx = 9;
		
		if (densitiesR>60 && ulx<9)
			ulx++;
		
		if (!Main.mygame.getBoat().isJumped())
		{
			for(int iuly = uly;iuly<=uly+1;iuly++)
				for(int iulx = ulx;iulx<=ulx;iulx++)
				{
					String token = Main.mygame.getRiverController().getMap().split(",")[iuly].substring(iulx,iulx+1)+"";
					
					//if the a digit occours
					if (isNumeric(token))
					{
						//increase score
						Main.mygame.getSound().playGain();
						Main.mygame.getBoat().setScore(Main.mygame.getBoat().getScore()+Integer.parseInt(token)*100);
					}
					if (token.equals("+"))
					{
						//increase health
						Main.mygame.getBoat().healed(15);
					}
					if (token.equals("#"))
					{
						//decrease health
						Main.mygame.getBoat().damaged();
					}
					
					if (token.equals("_"))
					{
						Main.mygame.getSound().playSplash();
						Main.mygame.getBoat().setJumped(true);
					}
					
					if (token.equals("_") || token.equals("#") || token.equals("+") || isNumeric(token)){
						Main.mygame.getRiverController().setMap(Main.mygame.getRiverController().getMap().substring(0,iuly*11+iulx)+"."+Main.mygame.getRiverController().getMap().substring(iuly*11+iulx+1));
					}
				}
		}
	}
}